package com.dahe.ysxt.vector.audit.thread;

import com.dahe.ysxt.vector.audit.dao.AuditDao;
import com.dahe.ysxt.vector.audit.dao.LikeAuditDao;
import com.dahe.ysxt.vector.audit.entity.AuditResult;
import com.dahe.ysxt.vector.audit.entity.LikeAuditResult;
import com.dahe.ysxt.vector.audit.service.AuditService;
import com.dahe.ysxt.vector.audit.service.impl.AuditServiceImpl;
import com.dahe.ysxt.vector.utils.SpringUtils;
import com.qcloud.cos.utils.CollectionUtils;
import com.tencent.tcvectordb.client.VectorDBClient;
import com.tencent.tcvectordb.model.param.enums.ReadConsistencyEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * 将相似度的计算结果，通过多线程方式批量插入表t_audit_qc_file_like_wjy
 */
public class AuditThread extends Thread{
    AuditService auditService = SpringUtils.getBean(AuditServiceImpl.class);
    AuditDao auditDao = SpringUtils.getBean(AuditDao.class);
    LikeAuditDao likeAuditDao = SpringUtils.getBean(LikeAuditDao.class);
    private Logger log = LoggerFactory.getLogger(this.getClass());

    public static BlockingQueue<AuditResult> queue = null;

    public static List<LikeAuditResult> dbLikeAuditResultList = new ArrayList<>();

    static int total = 0;


    @Override
    public void run() {
        while(true) {
            AuditResult auditResult = queue.poll();
            //连续取3次，如果都是空说明消费完了，线程终止
            if (auditResult == null) {
                try {
                    Thread.sleep(5000);
                    auditResult = queue.poll();
                    if (auditResult == null) {
                        Thread.sleep(5000);
                        auditResult = queue.poll();
                        if (auditResult == null) {
                            log.warn("线程已消费完");
                            return;
                        }
                    }
                } catch (InterruptedException e) {
                    log.error("线程消费异常", e);
                }
            }
            total++;
            LikeAuditResult likeAuditResult = null;
            try {
                likeAuditResult = auditService.queryByEmbedding(auditResult.getId(), auditResult.getErrorWords(), auditResult.getCorrectWords(), auditResult.getConText());
                likeAuditResult.setConTextMd5(auditResult.getConTextMd5());
                likeAuditResult.setResult(auditResult.getResult());
                likeAuditDao.save(likeAuditResult);
            } catch (Exception e) {
                log.error("发生异常ID:" + auditResult.getId() + ", 发生异常的错词：" + auditResult.getErrorWords());

            }


            if(total % 500 == 0) {
                log.info("已执行完第{}个", total);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName() + "我是主线程");
        //启动线程调用start()方法而不是run()方法 调用start()后线程变为就绪状态，而不是运行状态，还需等待CPU调度运行
//        new AuditThread().start();
//        new AuditThread().start();
        System.out.println("---主线程执行完毕---");
    }

}
